<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Wechat\AuthController;
use App\Http\Controllers\Wechat\VerifyCodeController;
use App\Http\Controllers\Wechat\OrderController;
use App\Http\Controllers\Wechat\ExchangeController;
use App\Http\Controllers\Wechat\ProductController;
use App\Http\Controllers\Wechat\MyController;
use App\Http\Controllers\Wechat\UserProfileController;
use App\Http\Controllers\Wechat\CouponController;
use App\Http\Controllers\PaymentController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// 微信小程序相关路由
Route::prefix('wechat')->group(function () {
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/verify-token', [AuthController::class, 'verifyToken']);
    Route::post('/verify-code', [VerifyCodeController::class, 'verify']);
    Route::get('/orders', [OrderController::class, 'index']);
    Route::post('/exchange', [ExchangeController::class, 'exchange']);
    
    // 用户资料相关路由
    Route::get('/user/profile/{token}', [UserProfileController::class, 'show'])
        ->name('wechat.user.profile')
        ->where('token', '[A-Za-z0-9]+')
        ->middleware(['throttle:60,1']);
        
    // 新增：用户资料更新路由
    Route::post('/user/update-profile/{token}/{nickName}', [UserProfileController::class, 'update'])
        ->name('wechat.user.update-profile')
        ->where([
            'token' => '[A-Za-z0-9]+',
            'nickName' => '[^/]+' // 允许除了斜杠外的所有字符
        ])
        ->middleware(['throttle:60,1']);
    
    // 优惠券相关路由
    Route::get('/coupons/{token}', [CouponController::class, 'index'])
        ->name('wechat.coupons.index')
        ->where('token', '[A-Za-z0-9]+');
    
    Route::get('/coupons/{token}/{couponId}', [CouponController::class, 'show'])
        ->name('wechat.coupons.show')
        ->where([
            'token' => '[A-Za-z0-9]+',
            'couponId' => '[0-9]+'
        ]);
});

Route::prefix('home')->group(function () {
    Route::get('products', [ProductController::class, 'index'])->middleware('member.permission');
});

// 微信小程序的个人中心相关路由
Route::prefix('wechat/my')->group(function () {
    // 不需要认证的路由
    Route::get('/orders-count/{token}', [MyController::class, 'getOrdersCount'])
        ->where('token', '[A-Za-z0-9]+');
    
    // 需要认证的路由
    // Route::middleware(['auth:api'])->group(function () {
        Route::get('/', [MyController::class, 'index']);
        Route::post('/profile', [MyController::class, 'updateProfile']);
    // });
});

// 微信支付回调路由
Route::post('/wechatpay/notify', [\App\Http\Controllers\Wechat\PaymentController::class, 'notify']);
// Route::post('/wechat/payment/notify', [\App\Http\Controllers\Wechat\PaymentController::class, 'notify']);
Route::post('/pay/jsapi', [PaymentController::class, 'jsapiPay']);

// 订单查询路由
Route::get('/orders/query/{orderNumber}', [\App\Http\Controllers\BackendOrderController::class, 'queryByOrderNumber'])
    ->where('orderNumber', '[A-Za-z0-9]+');

// 订单状态同步路由
Route::post('/orders/sync-wechat-status/{orderNumber}', [\App\Http\Controllers\BackendOrderController::class, 'syncWeChatPayStatus'])
    ->where('orderNumber', '[A-Za-z0-9]+');
